package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.R;
import saucaca.Object.CommentObject;
import saucaca.Util.ImageDisplayListener;
import saucaca.Util.TagName;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommentAdapter extends BaseAdapter {
	Context context;
	private LayoutInflater inflater;
	private List<CommentObject> list;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	private ImageDisplayListener imageListener;

	public CommentAdapter(Context context, List<CommentObject> _list) {
		TagName.showLogError("COMMENT ADAPTER", "list size:"+_list.size());
		list = _list;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
		imageListener = new ImageDisplayListener();
	}

	public void setList(ArrayList<CommentObject> _list) {
		list = _list;
		notifyDataSetChanged();
	}

	public List<CommentObject> getList() {
		return list;
	}

	class ViewHolder {
		ImageView imgeAvatar;
		TextView textComment;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_comment, null);
			holder.imgeAvatar = (ImageView) convertView.findViewById(R.id.item_comment_avatar);
			holder.textComment = (TextView) convertView.findViewById(R.id.item_comment_textContent);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.textComment.setText(list.get(position).getTXDES());
		imageLoader.displayImage(list.get(position).getAVATAR(), holder.imgeAvatar, options, imageListener);
		return convertView;
	}

}
